//
//  MJArrowFooter.m
//  HappyonlineUser
//
//  Created by admin on 2017/4/5.
//  Copyright © 2017年 肖世恒. All rights reserved.
//

#import "MJArrowFooter.h"
#import "NSBundle+MJRefresh.h"

@interface MJArrowFooter()

@property (strong, nonatomic) UIImageView *arrowloadingView;
@property (strong, nonatomic) UIActivityIndicatorView *loadingView;
@end

@implementation MJArrowFooter
#pragma mark - 懒加载子控件
- (UIImageView *)arrowloadingView
{
    if (!_arrowloadingView) {
        _arrowloadingView = [[UIImageView alloc] initWithImage:[NSBundle mj_arrowImage]];
        
    }
    return _arrowloadingView;
}


- (UIActivityIndicatorView *)loadingView
{
    if (!_loadingView) {
        _loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        _loadingView.hidesWhenStopped = YES;
        
    }
    return _loadingView;
}


#pragma mark - 重写父类的方法
- (void)prepare
{
    [super prepare];
    
    self.automaticallyHidden = YES;
    [self addSubview:self.arrowloadingView];
    [self addSubview:self.loadingView];
}

- (void)placeSubviews
{
    [super placeSubviews];
    
    // 箭头的中心点
    CGFloat arrowCenterX = self.mj_w * 0.5;
    if (!self.stateLabel.hidden) {
        arrowCenterX -= self.labelLeftInset + self.stateLabel.mj_textWith * 0.5;
    }
    CGFloat arrowCenterY = self.mj_h * 0.5;
    CGPoint arrowCenter = CGPointMake(arrowCenterX, arrowCenterY);
    
    // 箭头
    if (self.arrowloadingView.constraints.count == 0) {
        self.arrowloadingView.mj_size = self.arrowloadingView.image.size;
        self.arrowloadingView.center = arrowCenter;
    }
    
    // 圈圈
    if (self.loadingView.constraints.count == 0) {
        self.loadingView.center = arrowCenter;
    }
    
    self.arrowloadingView.tintColor = self.stateLabel.textColor;
}

- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState
    
    // 根据状态做事情
    if (state == MJRefreshStateIdle) {
        if (oldState == MJRefreshStateRefreshing) {
            self.arrowloadingView.transform = CGAffineTransformMakeRotation(0.000001 - M_PI);
            [UIView animateWithDuration:MJRefreshSlowAnimationDuration animations:^{
                self.loadingView.alpha = 0.0;
            } completion:^(BOOL finished) {
                self.loadingView.alpha = 1.0;
                [self.loadingView stopAnimating];
                
                self.arrowloadingView.hidden = NO;
                self.arrowloadingView.image = [NSBundle mj_arrowImage];
            }];
        } else {
            self.arrowloadingView.hidden = NO;
            self.arrowloadingView.image = [NSBundle mj_arrowImage];
            [self.loadingView stopAnimating];
            [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
                self.arrowloadingView.transform = CGAffineTransformMakeRotation(0.000001 - M_PI);
            }];
        }
    } else if (state == MJRefreshStatePulling) {
        self.arrowloadingView.hidden = NO;
        self.arrowloadingView.image = [NSBundle mj_arrowImage];
        [self.loadingView stopAnimating];
        [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
            self.arrowloadingView.transform = CGAffineTransformIdentity;
        }];
    } else if (state == MJRefreshStateRefreshing) {
        
        self.arrowloadingView.hidden = YES;
        self.arrowloadingView.image = nil;
        [self.loadingView startAnimating];
        
        
    } else if (state == MJRefreshStateNoMoreData) {
        self.arrowloadingView.hidden = YES;
        self.arrowloadingView.image = nil;
        [self.loadingView stopAnimating];
        
    }
}

@end
